package com.niiwoo.civet.trade.service.local.notify.impl;

import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.service.local.notify.AbstractNotifier;
import com.niiwoo.civet.user.enums.SMSTemplateKeyEnum;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractBorrowNotifier<T extends Project> extends AbstractNotifier<T> {

    @Autowired
    protected PrivacyMasks privacyMasks;

    @Override
    protected List<? extends UserNotifyInfo> getUserNotifyInfoList(T t) {
        BorrowUserNotifyInfo info = new BorrowUserNotifyInfo();
        info.setUserId(t.getBorrowUserId());
        info.setProjectId(t.getProjectId());
        info.setProjectTitle(t.getTitle());
        info.setMobileNo(privacyMasks.decryptPrivacy(t.getBorrowMobile()));
        info.setProjectFrom(t.getFromSource());
        info.setProjectType(t.getType());
        return Collections.singletonList(info);
    }

    @Override
    protected SMSRequest getSMSRequestDTO(UserNotifyInfo info) {
        return Optional.ofNullable(getSMSTemplateKey())
                        .map(key -> {
                            BorrowUserNotifyInfo borrowUserNotifyInfo = (BorrowUserNotifyInfo) info;
                            SMSRequest request = new SMSRequest();
                            request.setUserId(borrowUserNotifyInfo.getUserId());
                            // request.setMobileNo(borrowUserNotifyInfo.getMobileNo());
                            request.setSmsTemplateKeyEnum(key);
                            request.setParamList(this.getSMSParameterList(borrowUserNotifyInfo));
                            return request;
                        })
                        .orElse(null);
    }


    protected SMSTemplateKeyEnum getSMSTemplateKey() {
        return null;
    }

    protected List<Object> getSMSParameterList(BorrowUserNotifyInfo info) {
        return Collections.singletonList(info.getProjectTitle());
    }

    @Getter
    @Setter
    protected static class BorrowUserNotifyInfo extends UserNotifyInfo {

        private String userId;

        private Long projectId;

        private String projectTitle;

        private String mobileNo;

        private String pushContent;

        private Byte projectType;

        private Byte projectFrom;

    }

}
